#include "lcd.h"
#include "def.h"
#include "library.h"
#include "glib.h"
#include "sfr6410.h"

#include "lcdn43i.h"

//#define S3CFB_CLKVAL		11
//#define S3CFB_VIDCON1	(S3C_VIDCON1_IVCLK_RISE_EDGE)

//u32 uClkVal,uVidconReg,freq_lcdclk,freq_Hclk,fb_size;

static u32 LCDBuf[160000];

void LCDN43iInit(void)
{
    LCD_InitDISPC1(S3CFB_HRES, S3CFB_VRES, RGB18, (u32)LCDBuf, WIN0, false);
    LcdcOutp32(rVIDTCON0, VBPDE(0) |
               VBPD(S3CFB_VBP) |
               VFPD(S3CFB_VFP) |
               VSPW(S3CFB_VSW));
    LcdcOutp32(rVIDTCON1, VFPDE(0) |
               HBPD(S3CFB_HBP) |
               HFPD(S3CFB_HFP) |
               HSPW(S3CFB_HSW));
    LCD_SetWinOnOff(1, WIN0);
    LCD_Start();
    GLIB_Init((u32)LCDBuf, S3CFB_HRES, S3CFB_VRES, RGB24);
}

void LCDN43iDrawPicture(int fRGB, const char *data)
{
    u32 i, j;
    u32 tmp;
    u32 index = 0;

    if (fRGB) {
        for (j = 0; j < 272; ++j) {
            for (i = 0; i < 480; ++i) {
                tmp = (u32)data[index + 2];
                tmp |= (u32)data[index + 1] << 6;
                tmp |= (u32)data[index] << 12;;
                index += 3;
                GLIB_PutPixel2(i, j, tmp);
            }
        }
    } else {
        for (j = 0; j < 272; ++j) {
            for (i = 0; i < 480; ++i) {
                tmp = (u32)data[index + 2] << 12;
                tmp |= (u32)data[index + 1] << 6;
                tmp |= (u32)data[index];
                index += 3;
                GLIB_PutPixel2(i, j, tmp);
            }
        }
    }
}
